package com.taotao.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.pojo.TbItem;
import com.taotao.service.ExcelOperateService;

/**
 * 商品数据导出Excel数据
 * @author Administrator
 *
 */
@Controller
public class ExcelOperateController {
	@Autowired
	private ExcelOperateService excelOperateService;
	
	@RequestMapping("/item/export")
	@ResponseBody
	public TaotaoResult export(HttpServletResponse response) {
		response.setContentType("application/binary;charset=UTF-8");
		try {
			ServletOutputStream out = response.getOutputStream();
			String fileName = new String(
					("studentInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8");
			response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
			String[] titles = { "商品ID","商品标题 ","叶子类目 ","卖点 ","价格 ","库存数量 ","条形码 ","状态 ","创建日期 ","更新日期"};
			exportExcel(titles, out);
			return TaotaoResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500,"导出失败",null);
		}
	}
	
	public void exportExcel(String[] titles, ServletOutputStream out) {
		// 第一步，创建一个workbook，对应一个Excel文件
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet hssfSheet = workbook.createSheet("商品信息表");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow hssfRow = hssfSheet.createRow(0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
		// 居中样式
		hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		HSSFCell hssfCell = null;
		for (int i = 0; i < titles.length; i++) {
			hssfCell = hssfRow.createCell(i);// 列索引从0开始
			hssfCell.setCellValue(titles[i]);// 列名1
			hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
		}
		
		List<TbItem> itemList = excelOperateService.exportExcel();
	
		// 第五步根据返回的实体创建行和列，并填充各个单元格的值
		Iterator<TbItem> it = itemList.iterator();
		int i = 0;
		while (it.hasNext()) {
			TbItem tbItem = (TbItem) it.next();
			hssfRow = hssfSheet.createRow(i++);
			hssfRow.createCell(0).setCellValue(tbItem.getId());
			hssfRow.createCell(1).setCellValue(tbItem.getTitle());
			hssfRow.createCell(2).setCellValue(tbItem.getCid());
			hssfRow.createCell(3).setCellValue(tbItem.getSellPoint());
			hssfRow.createCell(4).setCellValue(tbItem.getPrice());
			hssfRow.createCell(5).setCellValue(tbItem.getNum());
			hssfRow.createCell(6).setCellValue(tbItem.getBarcode());
			hssfRow.createCell(7).setCellValue(tbItem.getStatus());
			hssfRow.createCell(8).setCellValue(tbItem.getCreated());
			hssfRow.createCell(9).setCellValue(tbItem.getUpdated());
		}
		// 第六步，将文件输出到客户端浏览器
		try {
			workbook.write(out);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
